package com.smart.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.Device;
import com.smart.mapper.DeviceMapper;
import com.smart.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 设备 服务实现类
 * </p>
 *
 * @author Cookies
 * @since 2024-05-14
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {
    @Override
    public IPage getDeviceList(Device condition, Integer current, Integer size) {
        IPage page = new Page(current, size);
        IPage deviceData;
        if (condition == null) {
            deviceData = lambdaQuery().select(Device::getId, Device::getType, Device::getSubType, Device::getName, Device::getStatus)
                    .page(page);
        } else {
            deviceData = lambdaQuery().select(Device::getId, Device::getType, Device::getSubType, Device::getName, Device::getStatus)
                    .eq(StrUtil.isNotBlank(condition.getType()), Device::getType, condition.getType())
                    .eq(StrUtil.isNotBlank(condition.getSubType()), Device::getSubType, condition.getSubType())
                    .eq(StrUtil.isNotBlank(condition.getName()), Device::getName, condition.getName())
                    .eq(condition.getStatus() != null, Device::getStatus, condition.getStatus())
                    .page(page);
        }
        return deviceData;
    }

    @Override
    public IPage getDeviceLocation(Integer current, Integer size) {
        IPage page = new Page(current, size);
        IPage locationData = lambdaQuery().select(Device::getId, Device::getName, Device::getLng, Device::getLat)
                .page(page);
        return locationData;
    }
}
